package com.badibadi.activity;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.badibadi.infos.ImageAttribute;
import com.badibadi.logic.ImgFileListActivity;
import com.badibadi.mytools.IOUtils;
import com.badibadi.mytools.SelectPicLayoutActivity;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class SelectPictsMail_Activity extends SelectPicLayoutActivity {
	private String timeString;// 返回的图片名称
	private String panduan;
	private String club;
	private String no_dou;
	private ArrayList<String> filelist;

	private final int PIC_PATH = 13;// 照片的路径
	public final static int HUICHUAN_KEY_1 = 32;// 图片回传

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		panduan = getIntent().getStringExtra("panduan");
		club = getIntent().getStringExtra("club");
		no_dou = getIntent().getStringExtra("no_dou");
		takePhotoBtn = (Button) findViewById(R.id.btn_take_photo);
		pickPhotoBtn = (Button) findViewById(R.id.btn_pick_photo);
		cancelBtn = (Button) findViewById(R.id.btn_cancel);
		filelist = new ArrayList<String>();
		takePhotoBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				/**
				 * 下面这句还是老样子，调用快速拍照功能，至于为什么叫快速拍照，大家可以参考如下官方
				 * 文档，you_sdk_path/docs/guide/topics/media/camera.html
				 * 我刚看的时候因为太长就认真看，其实是错的，这个里面有用的太多了，所以大家不要认为
				 * 官方文档太长了就不看了，其实是错的，这个地方小马也错了，必须改正
				 */
				Date date = new Date(System.currentTimeMillis());
				SimpleDateFormat dateFormat = new SimpleDateFormat(
						"'IMG'_yyyyMMddHHmmss");
				timeString = dateFormat.format(date);
				// 创建存储图片的路径，没有sd卡就不能使用照相功能
				if (!IOUtils.hasSDcard()) {
					Toast.makeText(SelectPictsMail_Activity.this,
							getResources().getString(R.string.l_xb139), 3000)
							.show();
					return;
				}
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
						Environment.getExternalStorageDirectory()
								+ "/DCIM/Camera", timeString + ".jpg")));
				startActivityForResult(intent, 22);
				// finish();
			}
		});
		pickPhotoBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SelectPictsMail_Activity.this,
						ImgFileListActivity.class);
				intent.putExtra("panduan", panduan);
				if (club != null) {
					intent.putExtra("club", club);
				}
				if (no_dou != null) {
					intent.putExtra("no_dou", no_dou);
				}
				// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				// startActivity(intent);
				startActivityForResult(intent, PIC_PATH);
				// finish();
			}
		});
		cancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
//		   
		// 这是相机的
		Intent intent = new Intent();
		if (requestCode == 22 && resultCode == -1) {
			filelist.add(Environment.getExternalStorageDirectory().getPath()
					+ "/DCIM/Camera/" + timeString + ".jpg");
			intent.putExtra("files", filelist);
			setResult(HUICHUAN_KEY_1, intent);
			this.finish();
		}

//		   
		// 这是本地的
		if (requestCode == PIC_PATH && resultCode == 5) {
			if (data != null) {
				filelist = data.getStringArrayListExtra("files");
				ImageAttribute attribute = (ImageAttribute) data
						.getSerializableExtra("img_info");
				intent.putExtra("files", filelist);
				if (attribute != null) {
					intent.putExtra("img_info", attribute);
				}
				setResult(HUICHUAN_KEY_1, intent);
				this.finish();
				//    
			}
		}
		// super.onActivityResult(requestCode, resultCode, data);
	}
}
